package com.cyx.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyx.reggie.common.BaseContext;
import com.cyx.reggie.common.R;
import com.cyx.reggie.dto.OrdersDto;
import com.cyx.reggie.entity.OrderDetail;
import com.cyx.reggie.entity.Orders;
import com.cyx.reggie.service.OrderDetailService;
import com.cyx.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 陈彦西
 * @date 2024/7/20 下午3:54
 */
@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 用户下单
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("订单数据：{}", orders);
        orderService.submit(orders);
        return R.success("下单成功");


    }

    /**
     * 分类信息分页查询
     * 分页构造器
     * 条件构造器，进行排序，
     * 添加排序条件
     * 进行分页查询
     * 返回值
     */
    @GetMapping("/page")
    public R<Page<Orders>> page(@RequestParam int page, @RequestParam int pageSize, Long id, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        if (id != null) {
            queryWrapper.eq(Orders::getId, id);
        }
        if (beginTime != null) {
            queryWrapper.ge(Orders::getOrderTime, beginTime);
        }
        if (endTime != null) {
            queryWrapper.le(Orders::getOrderTime, endTime);
        }
        queryWrapper.orderByDesc(Orders::getOrderTime);

        // 执行分页查询
        Page<Orders> resultPage = orderService.page(pageInfo, queryWrapper);

        return R.success(resultPage);
    }

    /**
     * 更新订单状态
     *
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Orders orders) {
        log.info(orders.toString());
        orderService.updateById(orders);


        return R.success("更新成功");

    }

    /**
     * 用户端分页查询
     *
     * @param page
     * @param pageSize
     * @return 订单dto
     */
    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize) {
        Long userId = BaseContext.getCurrentId();//获取用户id
        // 查询用户订单
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, userId);
        queryWrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(pageInfo, queryWrapper);


        // 新建dtopage 封装订单数据
        Page<OrdersDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo, dtoPage, "records");

        List<Orders> records = pageInfo.getRecords();

        // 迭代，将菜品详细表封装到订单里面
        List<OrdersDto> recordsDto = records.stream().map((item) -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item, ordersDto);
            //查询详细表
            LambdaQueryWrapper<OrderDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(OrderDetail::getOrderId, item.getNumber());
            List<OrderDetail> list = orderDetailService.list(lambdaQueryWrapper);//订单对应的详细菜品数据
            ordersDto.setOrderDetails(list);
            return ordersDto;

        }).collect(Collectors.toList());

        dtoPage.setRecords(recordsDto);


        return R.success(dtoPage);
    }

}








